//
//  NearVC.m
//  爱寻找
//
//  Created by qianfeng0 on 16/6/23.
//  Copyright © 2016年 王超. All rights reserved.
//

#import "NearVC.h"

#import <AFNetworking.h>
#import "NearModel.h"
#import "Helper.h"
#import "NearFunVc.h"
#import "LrdOutputView.h"
#import "TicketsVC.h"

@interface NearVC ()<LrdOutputViewDelegate>

//城市
@property (weak, nonatomic) IBOutlet UILabel *CityLabel;
//天气
@property (weak, nonatomic) IBOutlet UILabel *WeatherLabel;
//风
@property (weak, nonatomic) IBOutlet UILabel *WindLabel;
//气温
@property (weak, nonatomic) IBOutlet UILabel *TemperatureLabel;
//时间
@property (weak, nonatomic) IBOutlet UILabel *TimeLabel;

@property (nonatomic,strong) NSMutableArray * cityArray;
@property (nonatomic, strong) LrdOutputView * outputView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *height;

@end

@implementation NearVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    [self createButton];
    [self getData];
}
#pragma mark - 懒加载
- (NSMutableArray *)cityArray {
    if (_cityArray == nil) {
        _cityArray = [[NSMutableArray alloc] init];
    }
    return _cityArray;
}
#pragma mark - 初始化UI
- (void)initUI {
    
    self.navigationController.tabBarItem.selectedImage = [[UIImage imageNamed:@"iconfont-yewuyeicon14-2"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    _height.constant = ScreenWidth / 3;
    
    
    //添加弹出框数据源
    NSArray * array = @[@"北京",@"上海",@"广州",@"深圳"];
    for (int i = 0; i < array.count; i++) {
        
        LrdCellModel * model = [[LrdCellModel alloc] initWithTitle:array[i] imageName:nil];
        [self.cityArray addObject:model];
    }
    
    
}
#pragma mark - 获取数据
- (void)getData {
    
    AFHTTPSessionManager * mgr = [AFHTTPSessionManager manager];
    
    
    NSString * url = WEATHER_URL;
    
    [mgr GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray * array = responseObject[@"detail"];
        NSArray * modelArray = [NearModel arrayOfModelsFromDictionaries:array error:nil];
        NearModel * model = [modelArray lastObject];
        
        //赋值
        _CityLabel.text = model.city;
        _WeatherLabel.text = model.day_condition;
        _WindLabel.text = model.day_wind;
        _TemperatureLabel.text = [NSString stringWithFormat:@"%@-%@",model.night_temperature,model.day_temperature];
        _TimeLabel.text = model.date;
    
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
- (void)createButton {
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(ScreenWidth - 44, 44, 44, 44);
    [button setImage:[UIImage imageNamed:@"xiala"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
}
#pragma mark - button
- (void)onClick:(UIButton *)button {

    //弹出点 x = button的中心点
    CGFloat x = button.center.x;
    //弹出点 y = button的起始点+button的高度+10
    CGFloat y = button.frame.origin.y + button.bounds.size.height + 10;
    
    //第一个参数:模型数组
    //第二个参数:弹出的原点
    //第三个参数:cell的宽度
    //第四个参数:cell的高度
    //第五个参数:弹出的方向
    _outputView = [[LrdOutputView alloc] initWithDataArray:self.cityArray origin:CGPointMake(x, y) width:125 height:44 direction:kLrdOutputViewDirectionRight];
    
    _outputView.delegate = self;
    _outputView.dismissOperation = ^(){
        //设置成nil，以防内存泄露
        _outputView = nil;
    };
    [_outputView pop];
}
//点击弹出框的实现方法
- (void)didSelectedAtIndexPath:(NSIndexPath *)indexPath {
    
    NSLog(@"*****");
}

- (IBAction)ditu:(id)sender {
}
- (IBAction)erweima:(id)sender {
}
- (IBAction)haowan:(id)sender {
    
    //页面跳转
    UIStoryboard * stb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    NearFunVc * vc = [stb instantiateViewControllerWithIdentifier:@"NearFunVc"];
    
    //隐藏tabbar
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
    _outputView.delegate = self;
    _outputView.dismissOperation = ^(){
        //设置成nil，以防内存泄露
        _outputView = nil;
    };
    [_outputView pop];
    
    
}
- (IBAction)menpiao:(id)sender {
    
    UIStoryboard * stb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    TicketsVC * vc = [stb instantiateViewControllerWithIdentifier:@"TicketsVC"];
    
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (IBAction)zijia:(id)sender {
}




@end
